/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.AlwaysOnClusterBy;
/*     */ import com.zimbra.common.account.Key.CalendarResourceBy;
/*     */ import com.zimbra.common.account.Key.CosBy;
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.account.Key.UCServiceBy;
/*     */ import com.zimbra.common.account.Key.XMPPComponentBy;
/*     */ import com.zimbra.common.account.Key.ZimletBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.SetUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AlwaysOnCluster;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.CalendarResource;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.DynamicGroup;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.GlobalGrant;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.UCService;
/*     */ import com.zimbra.cs.account.XMPPComponent;
/*     */ import com.zimbra.cs.account.Zimlet;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.soap.admin.type.EffectiveRightsTargetSelector;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public enum TargetType
/*     */ {
/*  59 */   account(true, true, AttributeClass.account, com.zimbra.soap.type.TargetType.account, "Account"), 
/*  60 */   calresource(true, true, AttributeClass.calendarResource, com.zimbra.soap.type.TargetType.calresource, "CalendarResource"), 
/*  61 */   cos(true, false, AttributeClass.cos, com.zimbra.soap.type.TargetType.cos, "Cos"), 
/*  62 */   dl(true, true, AttributeClass.distributionList, com.zimbra.soap.type.TargetType.dl, "DistributionList"), 
/*  63 */   group(true, true, AttributeClass.group, com.zimbra.soap.type.TargetType.group, "DynamicGroup"), 
/*  64 */   domain(true, false, AttributeClass.domain, com.zimbra.soap.type.TargetType.domain, "Domain"), 
/*  65 */   server(true, false, AttributeClass.server, com.zimbra.soap.type.TargetType.server, "Server"), 
/*  66 */   alwaysoncluster(true, false, AttributeClass.alwaysOnCluster, com.zimbra.soap.type.TargetType.alwaysoncluster, "AlwaysOnCluster"), 
/*  67 */   ucservice(true, false, AttributeClass.ucService, com.zimbra.soap.type.TargetType.ucservice, "UCService"), 
/*  68 */   xmppcomponent(true, false, AttributeClass.xmppComponent, com.zimbra.soap.type.TargetType.xmppcomponent, "XMPPComponent"), 
/*  69 */   zimlet(true, false, AttributeClass.zimletEntry, com.zimbra.soap.type.TargetType.zimlet, "Zimlet"), 
/*  70 */   config(false, false, AttributeClass.globalConfig, com.zimbra.soap.type.TargetType.config, "GlobalConfig"), 
/*  71 */   global(false, false, AttributeClass.aclTarget, com.zimbra.soap.type.TargetType.global, "GlobalGrant");
/*     */   
/*     */ 
/*     */ 
/*     */   private boolean mNeedsTargetIdentity;
/*     */   
/*     */ 
/*     */   private boolean mIsDomained;
/*     */   
/*     */ 
/*     */   private AttributeClass mAttrClass;
/*     */   
/*     */ 
/*     */   private com.zimbra.soap.type.TargetType jaxbTargetType;
/*     */   
/*     */ 
/*     */   private String mPrettyName;
/*     */   
/*     */ 
/*     */   private Set<TargetType> mInheritedByTargetTypes;
/*     */   
/*     */ 
/*     */   private Set<TargetType> mInheritFromTargetTypes;
/*     */   
/*     */   private Set<TargetType> mSubTargetTypes;
/*     */   
/*     */ 
/*     */   static
/*     */   {
/* 100 */     init();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private TargetType(boolean NeedsTargetIdentity, boolean isDomained, AttributeClass attrClass, com.zimbra.soap.type.TargetType jaxbTargetType, String prettyName)
/*     */   {
/* 107 */     this.mNeedsTargetIdentity = NeedsTargetIdentity;
/* 108 */     this.mIsDomained = isDomained;
/* 109 */     this.mAttrClass = attrClass;
/* 110 */     this.jaxbTargetType = jaxbTargetType;
/* 111 */     this.mPrettyName = prettyName;
/*     */   }
/*     */   
/*     */   public com.zimbra.soap.type.TargetType toJaxb()
/*     */   {
/* 116 */     return this.jaxbTargetType;
/*     */   }
/*     */   
/*     */   public static TargetType fromJaxb(com.zimbra.soap.type.TargetType jaxbTT) {
/* 120 */     for (TargetType tt : ) {
/* 121 */       if (tt.toJaxb() == jaxbTT) {
/* 122 */         return tt;
/*     */       }
/*     */     }
/* 125 */     throw new IllegalArgumentException("Unrecognised TargetType" + jaxbTT);
/*     */   }
/*     */   
/*     */   private void setInheritedByTargetTypes(TargetType[] targetTypes) {
/* 129 */     this.mInheritedByTargetTypes = new HashSet(Arrays.asList(targetTypes));
/*     */   }
/*     */   
/*     */   static void init() {
/* 133 */     account.setInheritedByTargetTypes(new TargetType[] { account });
/*     */     
/*     */ 
/* 136 */     calresource.setInheritedByTargetTypes(new TargetType[] { calresource });
/*     */     
/*     */ 
/* 139 */     dl.setInheritedByTargetTypes(new TargetType[] { account, calresource, dl });
/*     */     
/*     */ 
/* 142 */     group.setInheritedByTargetTypes(new TargetType[] { account, calresource, group });
/*     */     
/*     */ 
/* 145 */     domain.setInheritedByTargetTypes(new TargetType[] { account, calresource, dl, group, domain });
/*     */     
/*     */ 
/* 148 */     cos.setInheritedByTargetTypes(new TargetType[] { cos });
/*     */     
/*     */ 
/* 151 */     server.setInheritedByTargetTypes(new TargetType[] { server });
/*     */     
/*     */ 
/* 154 */     alwaysoncluster.setInheritedByTargetTypes(new TargetType[] { alwaysoncluster });
/*     */     
/*     */ 
/* 157 */     ucservice.setInheritedByTargetTypes(new TargetType[] { ucservice });
/*     */     
/*     */ 
/* 160 */     xmppcomponent.setInheritedByTargetTypes(new TargetType[] { xmppcomponent });
/*     */     
/*     */ 
/* 163 */     zimlet.setInheritedByTargetTypes(new TargetType[] { zimlet });
/*     */     
/*     */ 
/* 166 */     config.setInheritedByTargetTypes(new TargetType[] { config });
/*     */     
/*     */ 
/* 169 */     global.setInheritedByTargetTypes(new TargetType[] { account, calresource, cos, dl, group, domain, server, alwaysoncluster, ucservice, xmppcomponent, zimlet, config, global });
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     TargetType inheritFrom;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 186 */     for (inheritFrom : values()) {
/* 187 */       inheritFrom.mInheritFromTargetTypes = new HashSet();
/* 188 */       inheritFrom.mSubTargetTypes = new HashSet();
/*     */       
/* 190 */       for (TargetType inheritedBy : values()) {
/* 191 */         if (inheritedBy.mInheritedByTargetTypes.contains(inheritFrom)) {
/* 192 */           inheritFrom.mInheritFromTargetTypes.add(inheritedBy);
/*     */         }
/*     */       }
/*     */       
/* 196 */       for (TargetType tt : inheritFrom.mInheritedByTargetTypes) {
/* 197 */         if (inheritFrom != tt) {
/* 198 */           inheritFrom.mSubTargetTypes.add(tt);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 203 */     for (TargetType tt : values()) {
/* 204 */       tt.mInheritedByTargetTypes = Collections.unmodifiableSet(tt.mInheritedByTargetTypes);
/* 205 */       tt.mInheritFromTargetTypes = Collections.unmodifiableSet(tt.mInheritFromTargetTypes);
/* 206 */       tt.mSubTargetTypes = Collections.unmodifiableSet(tt.mSubTargetTypes);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void dump()
/*     */   {
/* 217 */     System.out.println();
/* 218 */     System.out.println(this.mPrettyName);
/*     */     
/* 220 */     System.out.println("mInheritedByTargetTypes");
/* 221 */     for (TargetType tt : this.mInheritedByTargetTypes) {
/* 222 */       System.out.println("    " + tt);
/*     */     }
/*     */     
/* 225 */     System.out.println("mInheritFromTargetTypes");
/* 226 */     for (TargetType tt : this.mInheritFromTargetTypes) {
/* 227 */       System.out.println("    " + tt);
/*     */     }
/*     */     
/* 230 */     System.out.println("mSubTargetTypes");
/* 231 */     for (TargetType tt : this.mSubTargetTypes) {
/* 232 */       System.out.println("    " + tt);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean isInheritedBy(TargetType targetType)
/*     */   {
/* 243 */     return this.mInheritedByTargetTypes.contains(targetType);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   Set<TargetType> subTargetTypes()
/*     */   {
/* 255 */     return this.mSubTargetTypes;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   Set<TargetType> inheritFrom()
/*     */   {
/* 263 */     return this.mInheritFromTargetTypes;
/*     */   }
/*     */   
/*     */   public static boolean canBeInheritedFrom(Entry target) throws ServiceException {
/* 267 */     TargetType targetType = getTargetType(target);
/* 268 */     return !targetType.subTargetTypes().isEmpty();
/*     */   }
/*     */   
/*     */   public static TargetType fromCode(String s) throws ServiceException {
/*     */     try {
/* 273 */       return valueOf(s);
/*     */     } catch (IllegalArgumentException e) {
/* 275 */       throw ServiceException.INVALID_REQUEST("unknown target type: " + s, e);
/*     */     }
/*     */   }
/*     */   
/*     */   public String getCode() {
/* 280 */     return name();
/*     */   }
/*     */   
/*     */   public String getPrettyName() {
/* 284 */     return this.mPrettyName;
/*     */   }
/*     */   
/*     */   public boolean needsTargetIdentity() {
/* 288 */     return this.mNeedsTargetIdentity;
/*     */   }
/*     */   
/*     */   AttributeClass getAttributeClass() {
/* 292 */     return this.mAttrClass;
/*     */   }
/*     */   
/*     */   public static Entry lookupTarget(Provisioning prov, EffectiveRightsTargetSelector targSel) throws ServiceException
/*     */   {
/* 297 */     return lookupTarget(prov, targSel, true);
/*     */   }
/*     */   
/*     */   public static Entry lookupTarget(Provisioning prov, EffectiveRightsTargetSelector targSel, boolean mustFind) throws ServiceException
/*     */   {
/* 302 */     return lookupTarget(prov, fromJaxb(targSel.getType()), targSel.getBy(), targSel.getValue(), mustFind);
/*     */   }
/*     */   
/*     */   public static Entry lookupTarget(Provisioning prov, TargetType targetType, TargetBy targetBy, String target) throws ServiceException
/*     */   {
/* 307 */     return lookupTarget(prov, targetType, targetBy, target, true);
/*     */   }
/*     */   
/*     */   public static Entry lookupTarget(Provisioning prov, TargetType targetType, TargetBy targetBy, String target, boolean mustFind) throws ServiceException
/*     */   {
/* 312 */     return lookupTarget(prov, targetType, targetBy, target, false, mustFind);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Entry lookupTarget(Provisioning prov, TargetType targetType, TargetBy targetBy, String target, boolean needFullDL, boolean mustFind)
/*     */     throws ServiceException
/*     */   {
/* 329 */     Entry targetEntry = null;
/*     */     
/* 331 */     switch (targetType) {
/*     */     case account: 
/* 333 */       targetEntry = prov.get(Key.AccountBy.fromString(targetBy.name()), target);
/* 334 */       if ((targetEntry == null) && (mustFind)) {
/* 335 */         throw AccountServiceException.NO_SUCH_ACCOUNT(target);
/*     */       }
/*     */       break;
/*     */     case calresource: 
/* 339 */       targetEntry = prov.get(Key.CalendarResourceBy.fromString(targetBy.name()), target);
/* 340 */       if ((targetEntry == null) && (mustFind)) {
/* 341 */         throw AccountServiceException.NO_SUCH_CALENDAR_RESOURCE(target);
/*     */       }
/*     */       break;
/*     */     case dl: 
/* 345 */       if (needFullDL) {
/* 346 */         targetEntry = prov.getGroup(Key.DistributionListBy.fromString(targetBy.name()), target);
/*     */       } else {
/* 348 */         targetEntry = prov.getGroupBasic(Key.DistributionListBy.fromString(targetBy.name()), target);
/*     */       }
/* 350 */       if ((targetEntry == null) && (mustFind)) {
/* 351 */         throw AccountServiceException.NO_SUCH_DISTRIBUTION_LIST(target);
/*     */       }
/*     */       break;
/*     */     case group: 
/* 355 */       targetEntry = prov.getGroupBasic(Key.DistributionListBy.fromString(targetBy.name()), target);
/* 356 */       if ((targetEntry == null) && (mustFind)) {
/* 357 */         throw AccountServiceException.NO_SUCH_DISTRIBUTION_LIST(target);
/*     */       }
/*     */       break;
/*     */     case domain: 
/* 361 */       targetEntry = prov.get(Key.DomainBy.fromString(targetBy.name()), target);
/* 362 */       if ((targetEntry == null) && (mustFind)) {
/* 363 */         throw AccountServiceException.NO_SUCH_DOMAIN(target);
/*     */       }
/*     */       break;
/*     */     case cos: 
/* 367 */       targetEntry = prov.get(Key.CosBy.fromString(targetBy.name()), target);
/* 368 */       if ((targetEntry == null) && (mustFind)) {
/* 369 */         throw AccountServiceException.NO_SUCH_COS(target);
/*     */       }
/*     */       break;
/*     */     case server: 
/* 373 */       targetEntry = prov.get(Key.ServerBy.fromString(targetBy.name()), target);
/* 374 */       if ((targetEntry == null) && (mustFind)) {
/* 375 */         throw AccountServiceException.NO_SUCH_SERVER(target);
/*     */       }
/*     */       break;
/*     */     case alwaysoncluster: 
/* 379 */       targetEntry = prov.get(Key.AlwaysOnClusterBy.fromString(targetBy.name()), target);
/* 380 */       if ((targetEntry == null) && (mustFind)) {
/* 381 */         throw AccountServiceException.NO_SUCH_ALWAYSONCLUSTER(target);
/*     */       }
/*     */       break;
/*     */     case ucservice: 
/* 385 */       targetEntry = prov.get(Key.UCServiceBy.fromString(targetBy.name()), target);
/* 386 */       if ((targetEntry == null) && (mustFind)) {
/* 387 */         throw AccountServiceException.NO_SUCH_UC_SERVICE(target);
/*     */       }
/*     */       break;
/*     */     case xmppcomponent: 
/* 391 */       targetEntry = prov.get(Key.XMPPComponentBy.fromString(targetBy.name()), target);
/* 392 */       if ((targetEntry == null) && (mustFind)) {
/* 393 */         throw AccountServiceException.NO_SUCH_XMPP_COMPONENT(target);
/*     */       }
/*     */       break;
/*     */     case zimlet: 
/* 397 */       Key.ZimletBy zimletBy = Key.ZimletBy.fromString(targetBy.name());
/* 398 */       if (zimletBy != Key.ZimletBy.name) {
/* 399 */         throw ServiceException.INVALID_REQUEST("zimlet must be by name", null);
/*     */       }
/* 401 */       targetEntry = prov.getZimlet(target);
/* 402 */       if ((targetEntry == null) && (mustFind)) {
/* 403 */         throw AccountServiceException.NO_SUCH_ZIMLET(target);
/*     */       }
/*     */       break;
/*     */     case config: 
/* 407 */       targetEntry = prov.getConfig();
/* 408 */       break;
/*     */     case global: 
/* 410 */       targetEntry = prov.getGlobalGrant();
/* 411 */       break;
/*     */     default: 
/* 413 */       ServiceException.INVALID_REQUEST("invallid target type for lookupTarget:" + targetType.toString(), null);
/*     */     }
/*     */     
/* 416 */     return targetEntry;
/*     */   }
/*     */   
/*     */   public static Set<String> getAttrsInClass(Entry target) throws ServiceException {
/* 420 */     AttributeClass klass = getAttributeClass(target);
/* 421 */     return AttributeManager.getInstance().getAllAttrsInClass(klass);
/*     */   }
/*     */   
/*     */   static AttributeClass getAttributeClass(Entry target) throws ServiceException {
/* 425 */     return getTargetType(target).getAttributeClass();
/*     */   }
/*     */   
/*     */   public static TargetType getTargetType(Entry target) throws ServiceException
/*     */   {
/* 430 */     if ((target instanceof CalendarResource))
/* 431 */       return calresource;
/* 432 */     if ((target instanceof Account))
/* 433 */       return account;
/* 434 */     if ((target instanceof Domain))
/* 435 */       return domain;
/* 436 */     if ((target instanceof Cos))
/* 437 */       return cos;
/* 438 */     if ((target instanceof DistributionList))
/* 439 */       return dl;
/* 440 */     if ((target instanceof DynamicGroup))
/* 441 */       return group;
/* 442 */     if ((target instanceof Server))
/* 443 */       return server;
/* 444 */     if ((target instanceof AlwaysOnCluster))
/* 445 */       return alwaysoncluster;
/* 446 */     if ((target instanceof UCService))
/* 447 */       return ucservice;
/* 448 */     if ((target instanceof Config))
/* 449 */       return config;
/* 450 */     if ((target instanceof GlobalGrant))
/* 451 */       return global;
/* 452 */     if ((target instanceof Zimlet))
/* 453 */       return zimlet;
/* 454 */     if ((target instanceof XMPPComponent)) {
/* 455 */       return xmppcomponent;
/*     */     }
/* 457 */     throw ServiceException.FAILURE("internal error, target is : " + (target == null ? "null" : target.getClass().getCanonicalName()), null);
/*     */   }
/*     */   
/*     */   boolean isDomained()
/*     */   {
/* 462 */     return this.mIsDomained;
/*     */   }
/*     */   
/*     */   public boolean isGroup() {
/* 466 */     return (this == dl) || (this == group);
/*     */   }
/*     */   
/*     */   public static String getId(Entry target) {
/* 470 */     return (target instanceof NamedEntry) ? ((NamedEntry)target).getId() : null;
/*     */   }
/*     */   
/*     */   public static Domain getTargetDomain(Provisioning prov, Entry target)
/*     */     throws ServiceException
/*     */   {
/* 476 */     if ((target instanceof CalendarResource)) {
/* 477 */       CalendarResource cr = (CalendarResource)target;
/* 478 */       return prov.getDomain(cr); }
/* 479 */     if ((target instanceof Account)) {
/* 480 */       Account acct = (Account)target;
/* 481 */       return prov.getDomain(acct); }
/* 482 */     if ((target instanceof DistributionList)) {
/* 483 */       DistributionList dl = (DistributionList)target;
/* 484 */       return prov.getDomain(dl); }
/* 485 */     if ((target instanceof DynamicGroup)) {
/* 486 */       DynamicGroup group = (DynamicGroup)target;
/* 487 */       return prov.getDomain(group);
/*     */     }
/* 489 */     return null;
/*     */   }
/*     */   
/*     */   public static String getTargetDomainName(Provisioning prov, Entry target)
/*     */     throws ServiceException
/*     */   {
/* 495 */     if ((target instanceof CalendarResource)) {
/* 496 */       CalendarResource cr = (CalendarResource)target;
/* 497 */       return cr.getDomainName(); }
/* 498 */     if ((target instanceof Account)) {
/* 499 */       Account acct = (Account)target;
/* 500 */       return acct.getDomainName(); }
/* 501 */     if ((target instanceof DistributionList)) {
/* 502 */       DistributionList dl = (DistributionList)target;
/* 503 */       return dl.getDomainName(); }
/* 504 */     if ((target instanceof DynamicGroup)) {
/* 505 */       DynamicGroup group = (DynamicGroup)target;
/* 506 */       return group.getDomainName();
/*     */     }
/* 508 */     return null;
/*     */   }
/*     */   
/*     */   static String getSearchBase(Provisioning prov, TargetType tt)
/*     */     throws ServiceException
/*     */   {
/* 514 */     LdapDIT dit = ((LdapProv)prov).getDIT();
/*     */     
/*     */     String base;
/*     */     
/* 518 */     switch (tt) {
/*     */     case account: 
/*     */     case calresource: 
/*     */     case dl: 
/*     */     case group: 
/* 523 */       base = dit.mailBranchBaseDN();
/* 524 */       break;
/*     */     case domain: 
/* 526 */       base = dit.domainBaseDN();
/* 527 */       break;
/*     */     case cos: 
/* 529 */       base = dit.cosBaseDN();
/* 530 */       break;
/*     */     case server: 
/* 532 */       base = dit.serverBaseDN();
/* 533 */       break;
/*     */     case alwaysoncluster: 
/* 535 */       base = dit.alwaysOnClusterBaseDN();
/* 536 */       break;
/*     */     case ucservice: 
/* 538 */       base = dit.ucServiceBaseDN();
/* 539 */       break;
/*     */     case xmppcomponent: 
/* 541 */       base = dit.xmppcomponentBaseDN();
/* 542 */       break;
/*     */     case zimlet: 
/* 544 */       base = dit.zimletBaseDN();
/* 545 */       break;
/*     */     case config: 
/* 547 */       base = dit.configDN();
/* 548 */       break;
/*     */     
/*     */ 
/*     */ 
/*     */     case global: 
/* 553 */       base = dit.globalGrantDN();
/* 554 */       break;
/*     */     default: 
/* 556 */       throw ServiceException.FAILURE("internal error", null);
/*     */     }
/*     */     
/* 559 */     return base;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static Map<String, Set<String>> getSearchBasesAndOCs(Provisioning prov, Set<TargetType> targetTypes)
/*     */     throws ServiceException
/*     */   {
/* 599 */     if (targetTypes.isEmpty()) {
/* 600 */       return null;
/*     */     }
/* 602 */     Map<String, Set<String>> tempResult = new HashMap();
/*     */     
/* 604 */     for (TargetType tt : targetTypes) {
/* 605 */       String base = getSearchBase(prov, tt);
/*     */       
/* 607 */       String oc = tt.getAttributeClass().getOCName();
/* 608 */       Set<String> ocs = (Set)tempResult.get(base);
/* 609 */       if (ocs == null) {
/* 610 */         ocs = new HashSet();
/* 611 */         tempResult.put(base, ocs);
/*     */       }
/* 613 */       ocs.add(oc);
/*     */     }
/*     */     
/*     */ 
/* 617 */     LdapDIT dit = ((LdapProv)prov).getDIT();
/* 618 */     String configBranchBase = dit.configBranchBaseDN();
/* 619 */     Set<String> mailBranchOCs = new HashSet();
/* 620 */     Set<String> configBranchOCs = new HashSet();
/*     */     
/* 622 */     String leastCommonBaseInMailBranch = null;
/* 623 */     String leastCommonBaseInConfigBranch = null;
/*     */     
/* 625 */     for (Map.Entry<String, Set<String>> entry : tempResult.entrySet()) {
/* 626 */       String base = (String)entry.getKey();
/* 627 */       Set<String> ocs = (Set)entry.getValue();
/*     */       
/* 629 */       boolean inConfigBranch = base.endsWith(configBranchBase);
/* 630 */       if (inConfigBranch) {
/* 631 */         configBranchOCs.addAll(ocs);
/*     */         
/* 633 */         if (leastCommonBaseInConfigBranch == null) {
/* 634 */           leastCommonBaseInConfigBranch = base;
/*     */         } else {
/* 636 */           leastCommonBaseInConfigBranch = getCommonBase(base, leastCommonBaseInConfigBranch);
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 641 */         mailBranchOCs.addAll(ocs);
/*     */         
/* 643 */         if (leastCommonBaseInMailBranch == null) {
/* 644 */           leastCommonBaseInMailBranch = base;
/*     */         } else {
/* 646 */           leastCommonBaseInMailBranch = getCommonBase(base, leastCommonBaseInMailBranch);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 652 */     Map<String, Set<String>> result = new HashMap();
/*     */     
/*     */ 
/* 655 */     if ((LdapDIT.isZimbraDefault(dit)) && 
/* 656 */       (leastCommonBaseInMailBranch != null) && (leastCommonBaseInConfigBranch != null))
/*     */     {
/* 658 */       String commonBase = getCommonBase(leastCommonBaseInMailBranch, leastCommonBaseInConfigBranch);
/*     */       
/* 660 */       Set<String> allOCs = SetUtil.union(mailBranchOCs, configBranchOCs);
/* 661 */       result.put(commonBase, allOCs);
/* 662 */       return result;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 667 */     if (leastCommonBaseInMailBranch != null) {
/* 668 */       result.put(leastCommonBaseInMailBranch, mailBranchOCs);
/*     */     }
/* 670 */     if (leastCommonBaseInConfigBranch != null) {
/* 671 */       result.put(leastCommonBaseInConfigBranch, configBranchOCs);
/*     */     }
/*     */     
/* 674 */     return result;
/*     */   }
/*     */   
/*     */   static String getCommonBase(String dn1, String dn2) {
/* 678 */     String top = "";
/*     */     
/* 680 */     if ((top.equals(dn1)) || (top.equals(dn2))) {
/* 681 */       return top;
/*     */     }
/* 683 */     String[] rdns1 = dn1.split(",");
/* 684 */     String[] rdns2 = dn2.split(",");
/*     */     
/* 686 */     String[] shorter = rdns1.length < rdns2.length ? rdns1 : rdns2;
/*     */     
/* 688 */     int i = 0;
/* 689 */     while ((i < shorter.length) && 
/* 690 */       (rdns1[(rdns1.length - 1 - i)].equals(rdns2[(rdns2.length - 1 - i)])))
/*     */     {
/*     */ 
/* 693 */       i++;
/*     */     }
/*     */     
/* 696 */     StringBuilder sb = new StringBuilder();
/* 697 */     for (int j = shorter.length - i; j < shorter.length; j++) {
/* 698 */       if (j != shorter.length - i)
/* 699 */         sb.append(",");
/* 700 */       sb.append(shorter[j]);
/*     */     }
/*     */     
/* 703 */     return sb.toString();
/*     */   }
/*     */   
/*     */   static class SearchBaseAndOC
/*     */   {
/*     */     String mBase;
/*     */     List<String> mOCs;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/TargetType.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */